-- oUF_Basic
-- UF Style based off of oUF_Neav


-- different skins
-- to use default blizz skin set playerframeskin to true
-- to use lanacan's black skin set playerframeskin to false
local showParty = false
local framefolder
local borderskin       
local playerframeskin = false

if (playerframeskin) then
  framefolder = "TargetingFrame"
  borderskin = oUF_Basic.media.borderblizz
else
  framefolder = "TargetingFrames"
  borderskin = oUF_Basic.media.borderblack
end

-- p3lim
local string_format = string.format
local _, class = UnitClass('player')

local time
local _G = getfenv(0)

for _, v in pairs({
    InterfaceOptionsPartyRaidPanelPartyBackground,
    InterfaceOptionsPartyRaidPanelPartyPets,
    InterfaceOptionsCombatPanelTargetOfTarget,
    InterfaceOptionsCombatPanelTOTDropDown,
    InterfaceOptionsCombatPanelEnemyCastBarsOnPortrait,
}) do
    v:SetAlpha(0.35)
    v:Disable()
    v.Enable = v.Disable
end

-- p3lim
local colors = setmetatable({
	power = setmetatable({
		['MANA'] = {0, 144/255, 1},
	}, {__index = oUF.colors.power}),
}, {__index = oUF.colors})
   
local function CreateFont(parentName, font, fontSize, fontStyle, xShadow, yShadow)
	local f = parentName:CreateFontString(nil, "OVERLAY")
	f:SetFont(font, fontSize, fontStyle)
	f:SetShadowOffset(xShadow, yShadow)
	return f
end

local function menu(self)   
	local unit = self.unit:sub(1, -2)
	local cunit = self.unit:gsub("(.)", string.upper, 1)

	if (unit == "party" or unit == "partypet") then
		ToggleDropDownMenu(1, nil, _G["PartyMemberFrame"..self.id.."DropDown"], "cursor", 0, 0)
	elseif(_G[cunit.."FrameDropDown"]) then
		ToggleDropDownMenu(1, nil, _G[cunit.."FrameDropDown"], "cursor", 0, 0)
	end
end

local function NumFormat(i)
	if(i >= 1e6) then
		return ('%.1fm'):format(i / 1e6):gsub('%.?0+([km])$', '%1')
	elseif(i >= 1e4) then
		return ('%.1fk'):format(i / 1e3):gsub('%.?0+([km])$', '%1')
	else
		return i
	end
end

local function UpdateColor(self, unit, func)
	local colorA, colorB
	local _, class = UnitClass(unit)

    for i = 1, 4 do
        if (unit == "party"..i) then
            colorA = oUF.colors.class[class]
        end
    end

    if (unit == "pet" and GetPetHappiness()) then
		colorA = oUF.colors.happiness[GetPetHappiness()]
    elseif (unit == "player" or not UnitIsFriend("player", unit) and UnitIsPlayer(unit) and UnitClass(unit)) then
		colorA = oUF.colors.class[class]
	else
		colorB = {1, 1, 1}
	end

	if (colorA) then
		self:SetTextColor(colorA[1], colorA[2], colorA[3])
	elseif (colorB) then
		self:SetTextColor(colorB[1], colorB[2], colorB[3])
	end
end


local function OverrideUpdateLevel(self, unit, name)
    local level = UnitLevel(unit)
    local color = GetDifficultyColor(level)

    if (level < 0) then 
        self.Level:SetTextColor(1, 0, 0)
    else
        self.Level:SetTextColor(color.r, color.g, color.b)
    end

    -- frame skin
    if (UnitClassification(unit) == "elite") then
	self.Overlay:SetTexture("Interface\\".. framefolder .."\\UI-TargetingFrame-Elite")
    elseif (UnitClassification(unit) == "rareelite") then
       	self.Overlay:SetTexture("Interface\\".. framefolder .."\\UI-TargetingFrame-Rare-Elite")
    elseif (UnitClassification(unit) == "rare") then
       	self.Overlay:SetTexture("Interface\\".. framefolder .."\\UI-TargetingFrame-Rare")
    elseif (UnitClassification(unit) == "worldboss") then
       	self.Overlay:SetTexture("Interface\\".. framefolder .."\\UI-TargetingFrame-Elite")
    else
       	self.Overlay:SetTexture("Interface\\".. framefolder .."\\UI-TargetingFrame")
    end

end

local function OverrideUpdateName(self, event, unit)

    if (self.unit == unit) then
        if (self.Background) then
            if (UnitIsDead(unit) or UnitIsGhost(unit) or UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit)) then
                self.Background:SetVertexColor(0.5, 0.5, 0.5)
            elseif (UnitIsFriend(unit, "player") and UnitIsPlayer(unit)) then
                self.Background:SetVertexColor(RAID_CLASS_COLORS[select(2, UnitClass(unit))].r, RAID_CLASS_COLORS[select(2, UnitClass(unit))].g, RAID_CLASS_COLORS[select(2, UnitClass(unit))].b)
            elseif not UnitIsFriend(unit, "player") and UnitIsPlayer(unit) then 
                self.Background:SetVertexColor(0.9, 0, 0)
            else
                self.Background:SetVertexColor(UnitSelectionColor(unit))
            end
        end

        if (unit == "target" or unit == "player" or unit == "focus") then
            OverrideUpdateLevel(self, unit, name)
        end

	-- change names to uppercase (buggy atm though)
	-- local name = UnitName(unit)
	-- self.Name:SetText(string.upper(name))
        UpdateColor(self.Name, unit, "SetTextColor")
    end
end

local function PostUpdateHealth(self, event, unit, bar, min, max)
	self.Name:Show()
	if(UnitIsDead(unit)) then
		bar:SetValue(0)
		bar.value:SetText("Dead")
	elseif(UnitIsGhost(unit)) then
		bar:SetValue(0)
		bar.value:SetText("Ghost")
	elseif(not UnitIsConnected(unit)) then
		bar.value:SetText("Offline")
	else
		if(unit=="player") then
				if(min~=max) then
					bar.value:SetFormattedText("%d / %d", min, max)
				else
					bar.value:SetFormattedText("%d / %d", min, max)
				end
		elseif(unit=="pet" or unit=="partypet" or unit=="focus" or unit=="focustarget" or unit=="targettarget") then
			if(max==100) then
				if(min~=max) then
					bar.value:SetText(floor(min/max*100).."%")
				else
					bar.value:SetText()
				end
			elseif(min~=max) then
				bar.value:SetText(floor(min/max*100).."%")
			else
				bar.value:SetFormattedText("%s", NumFormat(max))
			end
		else
			if(max==100) then			
				if(unit=="target" and not UnitIsPlayer("target")) then
					bar.value:SetFormattedText("%d / %d", min, max)
				else
					bar.value:SetFormattedText("%d / %d", min, max)
				end
			else
				bar.value:SetFormattedText("%d / %d", min, max)
			end
		end
	end
    
    if (UnitIsDead(unit) or (UnitIsGhost(unit)) or not UnitIsConnected(unit)) then
        self.Health:SetStatusBarColor(0.5, 0.5, 0.5)
    else
        self.Health:SetStatusBarColor(0, 1, 0)
    end

   self:UNIT_NAME_UPDATE(event, unit)
end

local function PostUpdatePower(self, event, unit, bar, min, max)
    if (UnitIsDead(unit) or (UnitIsGhost(unit)) or not UnitIsConnected(unit)) then
        bar:SetValue(0)
        bar.value:SetText("")
    elseif (min == 0) then
        bar.value:SetText("")
    elseif (not UnitHasMana(unit)) then
        bar.value:SetText(min)
    elseif (min == max) then 
        bar.value:SetText(min.." / "..max)
    else
        if (UnitHasMana(unit)) then
            if (unit == "player") then
                bar.value:SetText(min.." / "..max)
            else
                bar.value:SetText(min.." / "..max)
            end
        else
            bar.value:SetText(min)
        end
    end
end

-- p3lim
local function UpdateDruidPower(self)
	local bar = self.DruidPower
	local num, str = UnitPowerType('player')
	local min = UnitPower('player', (num ~= 0) and 0 or 3)
	local max = UnitPowerMax('player', (num ~= 0) and 0 or 3)

	bar:SetMinMaxValues(0, max)

	if(min ~= max) then
		bar:SetValue(min)
		bar:SetAlpha(1)

		if(num ~= 0) then
			bar:SetStatusBarColor(unpack(colors.power['MANA']))
			--bar.Text:SetFormattedText('%d - %d%%', min, math.floor(min / max * 100))
			bar.Text:SetFormattedText('%d%%', math.floor(min / max * 100))
		else
			bar:SetStatusBarColor(unpack(colors.power['ENERGY']))
			bar.Text:SetText()
		end
	else
		bar:SetAlpha(0)
		bar.Text:SetText()
	end
end
--

local function PostCreateAuraIcon(self, button, icons)
     icons.showDebuffType = true
    
     button:SetWidth(8)
     button:SetHeight(8)
    
     button.cd:SetReverse()
     button.cd:ClearAllPoints()
     button.cd:SetPoint("TOPRIGHT", button, "TOPRIGHT", -1, -1)
     button.cd:SetPoint("BOTTOMLEFT", button, "BOTTOMLEFT", 1, 1)

     button.icon:SetTexCoord(0.04, 0.96, 0.04, 0.96)
     
     button.overlay:SetTexture(borderskin)
     button.overlay:SetTexCoord(0, 1, 0, 1)
     button.overlay:ClearAllPoints()
     button.overlay:SetPoint("TOPRIGHT", button, "TOPRIGHT", 2.5, 2.5)
     button.overlay:SetPoint("BOTTOMLEFT", button, "BOTTOMLEFT", -2.5, -2.5)
    
 	button.overlay.Hide = function(self) 
         self:SetVertexColor(0.65, 0.65, 0.65) 
     end
end

local UpdateInterval = 1
local Countdown	= 300
local DelayTime	= 0
local UntilAfkTempTime = 0

local function SecondsToTimeString(time)
    local myTime = tostring(math.floor(time % 60)).."s "
    if time >= 60 then 
        time = time / 60
        myTime = tostring(math.floor(time % 60))..'m '..myTime
        if time >= 60 then
            time = time / 60
            myTime = tostring(math.floor(time % 24))..'h '..myTime
            if time >= 24 then
                time = time / 24
                myTime = tostring(math.floor(time))..'d '..myTime
            end
        end
    end
    return myTime
end

local function frameStyle(self, unit)

    self.menu = menu
    self:EnableMouse(true)    
    self:SetFrameStrata((unit == "targettarget" or unit == "focustarget") and "MEDIUM" or "LOW")
    self:RegisterForClicks("anyup")
    self:SetAttribute("*type2", "menu")
    self:SetScript("OnEnter", UnitFrame_OnEnter)
    self:SetScript("OnLeave", UnitFrame_OnLeave)
    
    self.PostCreateAuraIcon = PostCreateAuraIcon

    self:SetAttribute("initial-height", (unit == "player" or unit == "target" or unit == "focus") and 42 or 30)
    self:SetAttribute("initial-width", (unit == "player" or unit == "target" or unit == "focus") and 175 or 105)

    ---------- health
    self.Health = CreateFrame("StatusBar", nil, self)
    self.Health:SetHeight((self:GetHeight() * 0.81))
    self.Health:SetStatusBarTexture(oUF_Basic.media.statusbar, "BORDER")
    self.Health:SetHeight(12)
    self.Health:SetWidth(119)
    self.PostUpdateHealth = PostUpdateHealth

    self.Health.value = CreateFont(self.Health, oUF_Basic.media.font, oUF_Basic.size.fontSmall * 1.1, nil, 1, -1)
    self.Health.value:SetPoint("CENTER", self.Health, 0, 1)

    ---------- runic power/mana/energy/rage
    self.Power = CreateFrame("StatusBar", nil, self)
    self.Power:SetStatusBarTexture(oUF_Basic.media.statusbar, "BORDER")
    self.Power:SetPoint("TOPLEFT", self.Health, "BOTTOMLEFT", 0, -1)
    self.Power:SetPoint("TOPRIGHT", self.Health, "BOTTOMRIGHT", 0, -1)
    self.Power.colorPower = true 
    self.Power.colorHappiness = true  
    self.PostUpdatePower = PostUpdatePower

    self.Power.value = CreateFont(self.Power, oUF_Basic.media.font, oUF_Basic.size.fontSmall * 1.1, nil, 1, -1)
    self.Power.value:SetPoint("CENTER", self.Power, 0, 1)

    if (unit == "targettarget" or unit == "focustarget") then
        self.Power.value:Hide()
    end
    
    ---------- background
    self.bg = self.Power:CreateTexture(nil, "BACKGROUND")
    self.bg:SetTexture(oUF_Basic.media.statusbar)
    self.bg:SetVertexColor(0, 0, 0, 0.35)
    self.bg:SetPoint("TOPRIGHT", self.Health, 0, (unit == "player") and 19 or 0)
    self.bg:SetPoint("BOTTOMLEFT", self.Power)
    
    ---------- name
    self.Name = CreateFont(self.Health, oUF_Basic.media.font, oUF_Basic.size.fontBig, nil, 1, -1)
    self.Name:SetJustifyH("CENTER")
    self.Name:SetHeight(10)
    self.Name:SetText("[name]")
    self:Tag(self.Name, "[name]")
    self.UNIT_NAME_UPDATE = OverrideUpdateName

    ---------- pvpicons
    if (unit == "focustarget") then
        -- do nothing
    else
        self.PvP = self.Health:CreateTexture(self:GetName().."PVPIcon", "OVERLAY", self)
        self.PvP:SetHeight(28)
        self.PvP:SetWidth(24)
    end

    ---------- portraits
    self.Portrait = self:CreateTexture(nil, "BACKGROUND")
    self.Portrait:SetWidth((unit == "player" or unit == "target" or unit == "focus") and 64 or 37)
    self.Portrait:SetHeight((self.Portrait:GetWidth()))
    
    ---------- masterlooter
    self.MasterLooter = self.Health:CreateTexture(nil, "OVERLAY")
    self.MasterLooter:SetHeight(12)
    self.MasterLooter:SetWidth(12)
    
    ---------- groupleader
    self.Leader = self.Health:CreateTexture(nil, "OVERLAY")
    self.Leader:SetHeight(12)
    self.Leader:SetWidth(12)
        
    ---------- raidicons
    self.RaidIcon = self.Health:CreateTexture(nil, "OVERLAY")
    self.RaidIcon:SetTexture("Interface\\TargetingFrame\\UI-RaidTargetingIcons", "OVERLAY")
    self.RaidIcon:SetHeight(13)
    self.RaidIcon:SetWidth(13)
    self.RaidIcon:SetPoint("CENTER", self.Portrait, "TOP", 0, -1)

    ---------- texture anchor
    self.f = CreateFrame("Frame", self:GetName().."TextureFrame", self)
    self.f:SetParent(self)

    ---------- set texture
    self.Overlay = self.Health:CreateTexture(nil, "MEDIUM")
    self.Overlay:SetParent(self.f)
    
    ---------- glow texture
    if (unit == "player" or self:GetParent():GetName():match("Party")) then
        self.Glow = self.Health:CreateTexture(self:GetName().."Glow", "BACKGROUND")
        self.Glow:SetAlpha(0)
    end

    ---------- healcomm limitations
    if (unit ~= "player" and unit ~= "target") then
        self.ignoreHealComm = true
    end
    
    ---------- player

    if (unit == "player") then
        self.f:SetHeight(100)
        self.f:SetWidth(232)
        self.f:SetPoint("CENTER", self, -20, -7)
        
	-- frame skin
        self.Overlay:SetTexture("Interface\\".. framefolder .."\\UI-TargetingFrame")
        self.Overlay:SetTexCoord(1, 0.09375, 0, 0.78125)
        self.Overlay:SetAllPoints(self.f)

	-- if you want to see a timer for your pvp status uncomment the below chunk of code        
        --if (oUF_Basic.show.pvpicons) then
        --    self.PvPTimer = self.Health:CreateFontString(self:GetName().."PVPTimer", "OVERLAY")
        --    self.PvPTimer:SetFont(oUF_Basic.media.font, oUF_Basic.size.fontSmall)
        --    self.PvPTimer:SetShadowOffset(1, -1)
        --    self.PvPTimer:SetPoint("CENTER", self.f, "TOPLEFT", 0, -30)
        --end
        
        self.Glow:SetTexture("Interface\\TargetingFrame\\UI-TargetingFrame-Flash")
        self.Glow:SetTexCoord(0.9453125, 0, 0, 0.181640625)
        self.Glow:SetPoint("TOPLEFT", self.f, "TOPLEFT", 13, 0)
        self.Glow:SetHeight(93)
        self.Glow:SetWidth(242)
        
        self.Leader:SetPoint("TOPLEFT", self.f, 45, -10)
        self.MasterLooter:SetPoint("TOPLEFT", self.f, 85, -10)

	self.Combat = self.Health:CreateTexture(nil, 'OVERLAY')
	self.Combat:SetHeight(10)
	self.Combat:SetWidth(10)
	self.Combat:SetPoint("TOPRIGHT", self.f, "TOPRIGHT", -7, -53)
	self.Combat:SetTexture('Interface\\CharacterFrame\\UI-StateIcon')
	self.Combat:SetTexCoord(0.58, 0.90, 0.08, 0.41)

        self.Level = CreateFont(self.Health, oUF_Basic.media.font, 14, "OUTLINE", 1, 0)
        self.Level:SetPoint("CENTER", self.f, -63, -16)
        self.Level:SetText("[level]")
        self:Tag(self.Level, "[level]")
        
        self.Name:SetPoint("CENTER", self.f, 50, 19)
        self.Portrait:SetPoint("TOPLEFT", self.f, 42, -12)
        self.PvP:SetPoint("TOPLEFT", self.f, 32, -40)
        self.Health:SetPoint("TOPLEFT", self.f, 106, -41)
        
        self:SetScale(oUF_Basic.unitframe.player.scale)

	-- p3lim
	if (class == 'DRUID') then
	    self.DruidPower = CreateFrame('statusbar', nil, self)
	    self.DruidPower:SetPoint('BOTTOM', Player, 'TOP', 0, 100)
	    self.DruidPower:SetStatusBarTexture(texture)
	    self.DruidPower:SetHeight(1)
	    self.DruidPower:SetWidth(260)
	    self.DruidPower:SetAlpha(0)
		
	    self.DruidPower.Text = CreateFont(self.Power, oUF_Basic.media.font, oUF_Basic.size.fontSmall * 1.1, nil, 1, -1)
	    self.DruidPower.Text:SetPoint('RIGHT', self.Power, 'RIGHT', 0, 2)

	    table.insert(self.__elements, UpdateDruidPower)
	    self:RegisterEvent('UNIT_MANA', UpdateDruidPower)
	    self:RegisterEvent('UNIT_ENERGY', UpdateDruidPower)
	end

	if (IsAddOnLoaded('oUF_Swing')) then
	    self.Swing = CreateFrame('StatusBar', nil, self)
	    self.Swing:SetPoint('TOPLEFT', self.Health, 'TOPLEFT', 5, -25)
	    self.Swing:SetPoint('BOTTOMRIGHT', self.Health, 'TOPRIGHT', 0, -26.5)
	    self.Swing:SetStatusBarTexture(oUF_Basic.media.statusbar)
	    self.Swing:SetStatusBarColor(.55, .57, .61)
	    self.Swing:SetBackdrop(backdrop)
	    self.Swing:SetBackdropColor(.15, .15, .15)

	    self.Swing.bg = self.Swing:CreateTexture(nil, 'BORDER')
	    self.Swing.bg:SetAllPoints(self.Swing)
	    self.Swing.bg:SetTexture(normTex)
	    self.Swing.bg:SetVertexColor(.15, .15, .15)
	end

    end
        
    ---------- pet

    if (unit == "pet") then
        
        self.f:SetHeight(53)
        self.f:SetWidth(128)
        self.f:SetPoint("CENTER", self, 1, -3)

	-- frame skin
        self.Overlay:SetTexture("Interface\\".. framefolder .."\\UI-SmallTargetingFrame")
        self.Overlay:SetPoint("TOPLEFT", self.f, 0, -2)
        self.Overlay:SetHeight(64)
        self.Overlay:SetWidth(128)
        
        self.Name:SetPoint("BOTTOMLEFT", self.Health, "TOPLEFT", 0, 5)
        self.Name:SetJustifyH("LEFT")
        self.Name:SetWidth(100)
        self.Name:SetHeight(10)
        
        self.Health:SetHeight(8)
        self.Health:SetWidth(69)
        self.Health:SetPoint("TOPLEFT", self.f, 46, -22)

        self.Portrait:SetPoint("TOPLEFT", self.f, 7, -6)

        self:SetScale(oUF_Basic.unitframe.pet.scale)

    end
    
    ---------- target + focus

    if (unit == "target" or unit == "focus") then
	if (unit == "target") then
	        self.Auras = CreateFrame("Frame", nil, self)
		self.Auras.gap = true
	        self.Auras.size = oUF_Basic.unitframe.target.auraSize
	        self.Auras:SetHeight(self.Auras.size * 3)
	        self.Auras:SetWidth(self.Auras.size * 6)
		self.Auras:SetPoint("TOPLEFT", self, "BOTTOMLEFT", -4, -7)
		self.Auras.initialAnchor = "TOPLEFT"
	        self.Auras["growth-x"] = "RIGHT"
	        self.Auras["growth-y"] = "DOWN"
		self.Auras.numBuffs = 40
		self.Auras.numDebuffs = 40
		self.Auras.spacing = -5

		self.CPoints = self:CreateFontString(nil, "OVERLAY")
		self.CPoints:SetFont(oUF_Basic.media.font, 14, "OUTLINE")
		self.CPoints:SetPoint('RIGHT', self, 'RIGHT', -23, -22)
		self.CPoints:SetTextColor(1, 1, 1)
		self.CPoints:SetJustifyH('RIGHT')
		--self.CPoints.unit = 'player'
	end
        
        self.f:SetHeight(100)
        self.f:SetWidth(232)
        self.f:SetPoint("CENTER", self, 20, -7)

	-- frame skin
        self.Overlay:SetTexture("Interface\\".. framefolder .."\\UI-TargetingFrame")
        self.Overlay:SetTexCoord(0.09375, 1, 0, 0.78125)
        self.Overlay:SetAllPoints(self.f)
        
        self.Background = self.Health:CreateTexture(self:GetName().."Background", "BACKGROUND")
        self.Background:SetTexture("Interface\\AddOns\\oUF_Basic\\media\\nameBackground")
        self.Background:SetTexCoord(0.2, 0.8, 0.3, 0.7)
        self.Background:SetHeight(18)
        self.Background:SetPoint("BOTTOMRIGHT", self.Health, "TOPRIGHT")
        self.Background:SetPoint("BOTTOMLEFT", self.Health, "TOPLEFT") 

        self.Leader:SetPoint("TOPRIGHT", self.f, -45, -10)
        
        self.MasterLooter:SetPoint("TOPRIGHT", self.f, -85, -10)

        self.Level = CreateFont(self.Health, oUF_Basic.media.font, 14, "OUTLINE", 1, 0)
        self.Level:SetPoint("CENTER", self.f, 63, -16)
        self.Level:SetText("[level]")
        self:Tag(self.Level, "[level]")

        self.CombatFeedbackText = CreateFont(self.Health, oUF_Basic.media.font, 18, "OUTLINE", 0, 0)
        self.CombatFeedbackText:SetPoint("CENTER", self.Portrait)
        
        self.Portrait:SetPoint("TOPRIGHT", self.f, -42, -12)
        self.PvP:SetPoint("TOPRIGHT", self.f, -23, -40)
        self.Health:SetPoint("TOPRIGHT", self.f, -106, -41)
        
        self.Name:SetPoint("CENTER", self.f, -52, 19)
        self.Name:SetWidth(110)
        
	if (unit == "target") then
	    self:SetScale(oUF_Basic.unitframe.target.scale)
        elseif (unit == "focus") then
            self:SetScale(oUF_Basic.unitframe.focus.scale)
        end

	-- makes the focus frame movable, uncomment to use it
	if (unit == "focus") then
            --self:SetMovable(true)
            --self:RegisterForDrag("LeftButton")
        
            --self:SetScript("OnDragStart", function(self)
            --    if (IsShiftKeyDown()) then
            --        self:ClearAllPoints() 
            --        self:StartMoving() 
            --    end
            --end)

            --self:SetScript("OnDragStop", function(self) 
            --    self:StopMovingOrSizing()
            --end)
	end
    end
    
    ---------- targetstarget

    if (unit == "targettarget") then
        self:SetAttribute("initial-height", 20)
	self:SetAttribute("initial-width", 85)

	-- frame skin
        self.Overlay:SetTexture("Interface\\".. framefolder .."\\UI-TargetofTargetFrame")
        self.Overlay:SetTexCoord(0.015625, 0.7265625, 0, 0.703125)
        self.Overlay:SetPoint("CENTER", self, 0, 0)
        self.Overlay:SetHeight(45)
        self.Overlay:SetWidth(93)
        
        self.Name:SetPoint("TOPLEFT", self, "CENTER", -3, -11)
        self.Name:SetJustifyH("LEFT")
        self.Name:SetWidth(100)
        
        self.Health:SetHeight(7)
        self.Health:SetWidth(47)
        self.Health:SetPoint("CENTER", self, 22, 4)

        self.Health.value:SetPoint("CENTER", self, "CENTER", 23, 1)

        self.Portrait:SetPoint("LEFT", self, "CENTER", -43, 0)
        
        self:SetScale(oUF_Basic.unitframe.targetstarget.scale)
    end

    ---------- focustarget
            
    if (unit == "focustarget") then
        self:SetAttribute("initial-height", 20)
	self:SetAttribute("initial-width", 85)

	-- frame skin
        self.Overlay:SetTexture("Interface\\".. framefolder .."\\UI-TargetofTargetFrame")
        self.Overlay:SetTexCoord(0.015625, 0.7265625, 0, 0.703125)
        self.Overlay:SetPoint("CENTER", self, 0, 0)
        self.Overlay:SetHeight(45)
        self.Overlay:SetWidth(93)
        
        self.Name:SetPoint("TOPLEFT", self, "CENTER", -3, -11)
        self.Name:SetJustifyH("LEFT")
        self.Name:SetWidth(100)
        
        self.Health:SetHeight(7)
        self.Health:SetWidth(47)
        self.Health:SetPoint("CENTER", self, 22, 4)

        self.Health.value:SetPoint("CENTER", self, "CENTER", 23, 1)

        self.Portrait:SetPoint("LEFT", self, "CENTER", -43, 0)

        self:SetScale(oUF_Basic.unitframe.focustarget.scale)

    end

    ---------- party
            
    if (self:GetParent():GetName():match("Party")) then
        self.Debuffs = CreateFrame("Frame", nil, self)
        self.Debuffs:SetFrameStrata("BACKGROUND")
        self.Debuffs:SetHeight(8)
        self.Debuffs:SetWidth(150)
        self.Debuffs.size = oUF_Basic.unitframe.party.auraSize
        self.Debuffs.spacing = -5
        self.Debuffs:SetPoint("LEFT", self, "RIGHT", 7, 10)
        self.Debuffs.initialAnchor = "LEFT"
        self.Debuffs["growth-y"] = "DOWN"
        self.Debuffs["growth-x"] = "RIGHT"
        self.Debuffs.num = 15
        
        self.f:SetHeight(53)
        self.f:SetWidth(128)
        self.f:SetPoint("CENTER", self, 1, 2)

	-- frame skin
        self.Overlay:SetTexture("Interface\\".. framefolder .."\\UI-PartyFrame")
        self.Overlay:SetPoint("TOPLEFT", self.f, 0, -2)
        self.Overlay:SetHeight(64)
        self.Overlay:SetWidth(128)

        self.Leader:SetPoint("TOPLEFT", self.f, 2, 0)
        
        self.MasterLooter:SetPoint("TOPLEFT", self.f, 29, 0)

        self.Name:SetPoint("TOPLEFT", self.Power, "TOPLEFT", -1, 22)
        self.Name:SetJustifyH("LEFT")
        self.Name:SetWidth(100)
        
        self.Health:SetPoint("TOPLEFT", self.f, 47, -12)
        self.Health:SetHeight(8)
        self.Health:SetWidth(70)
    
        self.Portrait:SetPoint("TOPLEFT", self.f, 7, -6)
        
        self.PvP:SetPoint("TOPLEFT", self.f, -9, -10)
        self.PvP:SetHeight(23)
        self.PvP:SetWidth(23)

        self.Glow:SetTexture("Interface\\TargetingFrame\\UI-PartyFrame-Flash")
        self.Glow:SetHeight(63)
        self.Glow:SetWidth(128)
        self.Glow:SetPoint("TOPLEFT", self.f, -3, 2)
        
        self:SetScale(oUF_Basic.unitframe.party.scale)
    end

    if (unit == "partypet1" or unit == "partypet2" or unit == "partypet3" or unit == "partypet4") then
        
        self.f:SetHeight(53)
        self.f:SetWidth(128)
        self.f:SetPoint("CENTER", self, 1, -3)

	-- frame skin
        self.Overlay:SetTexture("Interface\\".. framefolder .."\\UI-SmallTargetingFrame")
        self.Overlay:SetPoint("TOPLEFT", self.f, 0, -2)
        self.Overlay:SetHeight(64)
        self.Overlay:SetWidth(128)
        
        self.Name:SetPoint("BOTTOMLEFT", self.Health, "TOPLEFT", 0, 5)
        self.Name:SetJustifyH("LEFT")
        self.Name:SetWidth(100)
        self.Name:SetHeight(10)
        
        self.Health:SetHeight(8)
        self.Health:SetWidth(69)
        self.Health:SetPoint("TOPLEFT", self.f, 46, -22)

        self.Portrait:SetPoint("TOPLEFT", self.f, 7, -6)

	if (oUF_Basic.show.party) then
            self:SetScale(oUF_Basic.unitframe.pet.scale)
	else
	    self:SetScale(0.01)
	end
    end
        
    if (unit == "player" and oUF_Basic.unitframe.player.mouseoverText or unit == "pet" and oUF_Basic.unitframe.pet.mouseoverText or unit == "target" and oUF_Basic.unitframe.target.mouseoverText or unit == "focus" and oUF_Basic.unitframe.focus.mouseoverText or self:GetParent():GetName():match("Party") and oUF_Basic.unitframe.party.mouseoverText) then
        self.Health.value:Hide()
        self.Power.value:Hide()
        
        self:SetScript("OnEnter", function(self) 
            self.Health.value:Show() 
            self.Power.value:Show()
            UnitFrame_OnEnter(self) 
        end) 
        
        self:SetScript("OnLeave", function(self) 
            self.Health.value:Hide() 
            self.Power.value:Hide()
            UnitFrame_OnLeave(self) 
        end)
    end
    
    self.Power:SetHeight((self.Health:GetHeight() - 1))
    
    if (not unit == "focustarget") then
        self.PvP:SetWidth((self.PvP:GetHeight()))
    end
    
    if (not oUF_Basic.show.pvpicons) then
        self.PvP:Hide()
        self.PvP.Show = function() end
    end
    
    if (unit == "pet" or self:GetParent():GetName():match("Party") or unit == "partypet1" or unit == "partypet2" or unit == "partypet3" or unit == "partypet4") then
	self.Range = true
	self.inRangeAlpha = 1
	self.outsideRangeAlpha = 0.6
    end
    
    self.disallowVehicleSwap = true
    
    ---------- castbars

    if (unit == "player" or unit == "target" or unit == "focus" or unit == "pet" and oUF_Basic.show.castbars) then
	self.Castbar = CreateFrame("StatusBar", self:GetName().."Castbar", self)
	self.Castbar:SetStatusBarTexture(oUF_Basic.media.statusbar)
	self.Castbar:SetParent(self)

       	self.Castbar.bg = self.Castbar:CreateTexture(nil, "BACKGROUND")
	self.Castbar.bg:SetTexture(oUF_Basic.media.statusbar)
	self.Castbar.bg:SetAllPoints(self.Castbar)
       	self.Castbar.bg:SetVertexColor(0.2, 0.2, 0.2, 1)

       	if (unit == "player") then
       	    local playerColor = RAID_CLASS_COLORS[select(2, UnitClass("player"))]
       	    if (oUF_Basic.castbar.player.classcolor) then
                self.Castbar:SetStatusBarColor(playerColor.r, playerColor.g, playerColor.b)
            else
                self.Castbar:SetStatusBarColor(oUF_Basic.castbar.player.color[1], oUF_Basic.castbar.player.color[2], oUF_Basic.castbar.player.color[3])
	    end
            self.Castbar:SetPoint(oUF_Basic.castbar.player.position[1], oUF_Basic.castbar.player.position[2], oUF_Basic.castbar.player.position[3],oUF_Basic.castbar.player.position[4], oUF_Basic.castbar.player.position[5])
            self.Castbar:SetHeight(oUF_Basic.castbar.player.height)
            self.Castbar:SetWidth(oUF_Basic.castbar.player.width)
	    -- show latency on castbar
	    self.Castbar.SafeZone = self.Castbar:CreateTexture(nil, "OVERLAY") 
	    self.Castbar.SafeZone:SetTexture(texture)
	    self.Castbar.SafeZone:SetVertexColor(1, 0, 0, 1)
	    self.Castbar.SafeZone:SetPoint("TOPRIGHT")
	    self.Castbar.SafeZone:SetPoint("BOTTOMRIGHT")
            self.Castbar.SafeZone:SetAlpha(0.2)
        elseif (unit == "target") then
            self.Castbar:SetStatusBarColor(oUF_Basic.castbar.target.color[1], oUF_Basic.castbar.target.color[2], oUF_Basic.castbar.target.color[3])
            self.Castbar:SetPoint(oUF_Basic.castbar.target.position[1], oUF_Basic.castbar.target.position[2], oUF_Basic.castbar.target.position[3],oUF_Basic.castbar.target.position[4], oUF_Basic.castbar.target.position[5])
            self.Castbar:SetHeight(oUF_Basic.castbar.target.height)
            self.Castbar:SetWidth(oUF_Basic.castbar.target.width)
        elseif (unit == "focus") then
            self.Castbar:SetStatusBarColor(oUF_Basic.castbar.focus.color[1], oUF_Basic.castbar.focus.color[2], oUF_Basic.castbar.focus.color[3])
            self.Castbar:SetPoint(oUF_Basic.castbar.focus.position[1], oUF_Basic.castbar.focus.position[2], oUF_Basic.castbar.focus.position[3],oUF_Basic.castbar.focus.position[4], oUF_Basic.castbar.focus.position[5])
            self.Castbar:SetHeight(oUF_Basic.castbar.focus.height)
            self.Castbar:SetWidth(oUF_Basic.castbar.focus.width)
        elseif (unit == "pet") then
            self.Castbar:SetStatusBarColor(oUF_Basic.castbar.pet.color[1], oUF_Basic.castbar.pet.color[2], oUF_Basic.castbar.pet.color[3])
            self.Castbar:SetPoint(oUF_Basic.castbar.pet.position[1], oUF_Basic.castbar.pet.position[2], oUF_Basic.castbar.pet.position[3],oUF_Basic.castbar.pet.position[4], oUF_Basic.castbar.pet.position[5])
            self.Castbar:SetHeight(oUF_Basic.castbar.focus.height)
            self.Castbar:SetWidth(oUF_Basic.castbar.pet.width)
       	end

	-- positioning help (cast time)
	-- playerX: -3 | playerY: 0
	-- targetX: 1 | targetY: 0
	self.Castbar.Time = CreateFont(self.Health, oUF_Basic.media.font, oUF_Basic.size.fontSmall + 1, nil, 1, -1)
        self.Castbar.Time:SetPoint((unit == "target" and "LEFT") or "RIGHT", self.Castbar, (unit == "target" and "LEFT") or "RIGHT", (unit == "target" and 1) or -3, (unit == "target" and 0) or 0)
	self.Castbar.Time:SetJustifyH("RIGHT")
        self.Castbar.Time:SetParent(self.Castbar)

	-- displays time left instead of duration on casts
        self.Castbar.CustomTimeText = function(self, duration)
          if self.casting then
            self.Time:SetFormattedText("%.1f", self.max - duration)
          elseif self.channeling then
            self.Time:SetFormattedText("%.1f", duration)
          end
        end
	-- credit: Lyn --
        
	-- positioning help (cast name)
	-- playerX: 1 | playerY: 0
	-- targetX: -1 | targetY: 0
        self.Castbar.Text = CreateFont(self.Health, oUF_Basic.media.font, oUF_Basic.size.fontSmall + 1, nil, 1, -1)
        self.Castbar.Text:SetPoint((unit == "target" and "RIGHT") or "LEFT", self.Castbar, (unit == "target" and "RIGHT") or "LEFT", (unit == "target" and -1) or 1, (unit == "target" and 0) or 0)
        self.Castbar.Text:SetJustifyH("CENTER")
        self.Castbar.Text:SetParent(self.Castbar)

        self.TopLeft = self:CreateTexture(nil, "OVERLAY")
        self.TopLeft:SetTexture(borderskin)
        self.TopLeft:SetParent(self.Castbar)
        self.TopLeft:SetTexCoord(0, 1/3, 0, 1/3)
        self.TopLeft:SetPoint("TOPLEFT", self.Castbar, -3, 4)
        self.TopLeft:SetWidth(8)
        self.TopLeft:SetHeight(8)
        self.TopLeft:SetVertexColor(0.65, 0.65, 0.65)

        self.TopRight = self:CreateTexture(nil, "OVERLAY")
        self.TopRight:SetTexture(borderskin)
        self.TopRight:SetParent(self.Castbar)
        self.TopRight:SetTexCoord(2/3, 1, 0, 1/3)
        self.TopRight:SetPoint("TOPRIGHT", self.Castbar, 4, 4)
        self.TopRight:SetWidth(8) 
        self.TopRight:SetHeight(8)
        self.TopLeft:SetVertexColor(0.65, 0.65, 0.65)

        self.BottomLeft = self:CreateTexture(nil, "OVERLAY")
        self.BottomLeft:SetTexture(borderskin)
        self.BottomLeft:SetParent(self.Castbar)
        self.BottomLeft:SetTexCoord(0, 1/3, 2/3, 1)
        self.BottomLeft:SetPoint("BOTTOMLEFT", self.Castbar, -3, -4)
        self.BottomLeft:SetWidth(8) 
        self.BottomLeft:SetHeight(8)

        self.TopLeft:SetVertexColor(0.65, 0.65, 0.65)
        self.BottomRight = self:CreateTexture(nil, "OVERLAY")
        self.BottomRight:SetTexture(borderskin)
        self.BottomRight:SetParent(self.Castbar)
        self.BottomRight:SetTexCoord(2/3, 1, 2/3, 1)
        self.BottomRight:SetPoint("BOTTOMRIGHT", self.Castbar, 4, -4)
        self.BottomRight:SetWidth(8) 
        self.BottomRight:SetHeight(8)
        self.TopLeft:SetVertexColor(0.65, 0.65, 0.65)

        self.TopEdge = self:CreateTexture(nil, "OVERLAY")
        self.TopEdge:SetTexture(borderskin)
        self.TopEdge:SetParent(self.Castbar)
        self.TopEdge:SetTexCoord(1/3, 2/3, 0, 1/3)
        self.TopEdge:SetPoint("TOPLEFT", self.TopLeft, "TOPRIGHT")
        self.TopEdge:SetPoint("TOPRIGHT", self.TopRight, "TOPLEFT")
        self.TopEdge:SetHeight(8)
        self.TopLeft:SetVertexColor(0.65, 0.65, 0.65)
                
        self.BottomEdge = self:CreateTexture(nil, "OVERLAY")
        self.BottomEdge:SetTexture(borderskin)
        self.BottomEdge:SetParent(self.Castbar)
        self.BottomEdge:SetTexCoord(1/3, 2/3, 2/3, 1)
        self.BottomEdge:SetPoint("BOTTOMLEFT", self.BottomLeft, "BOTTOMRIGHT")
        self.BottomEdge:SetPoint("BOTTOMRIGHT", self.BottomRight, "BOTTOMLEFT")
        self.BottomEdge:SetHeight(8)
        self.TopLeft:SetVertexColor(0.65, 0.65, 0.65)

        self.LeftEdge = self:CreateTexture(nil, "OVERLAY")
        self.LeftEdge:SetTexture(borderskin)
        self.LeftEdge:SetParent(self.Castbar)
        self.LeftEdge:SetTexCoord(0, 1/3, 1/3, 2/3)
        self.LeftEdge:SetPoint("TOPLEFT", self.TopLeft, "BOTTOMLEFT")
        self.LeftEdge:SetPoint("BOTTOMLEFT", self.BottomLeft, "TOPLEFT")
        self.LeftEdge:SetWidth(8)
        self.TopLeft:SetVertexColor(0.65, 0.65, 0.65)
                
        self.RightEdge = self:CreateTexture(nil, "OVERLAY")
        self.RightEdge:SetTexture(borderskin)
        self.RightEdge:SetParent(self.Castbar)
        self.RightEdge:SetTexCoord(2/3, 1, 1/3, 2/3)
        self.RightEdge:SetPoint("TOPRIGHT", self.TopRight, "BOTTOMRIGHT")
        self.RightEdge:SetPoint("BOTTOMRIGHT", self.BottomRight, "TOPRIGHT")
        self.RightEdge:SetWidth(8)
        self.TopLeft:SetVertexColor(0.65, 0.65, 0.65)

	end
    
	return self
end

oUF:RegisterStyle("bUnits", frameStyle)
oUF:SetActiveStyle("bUnits")

local player = oUF:Spawn("player", "Player")
player:SetPoint(oUF_Basic.unitframe.player.position[1], oUF_Basic.unitframe.player.position[2], oUF_Basic.unitframe.player.position[3], oUF_Basic.unitframe.player.position[4], oUF_Basic.unitframe.player.position[5])

local pet = oUF:Spawn("pet", "Pet")
pet:SetPoint(oUF_Basic.unitframe.pet.position[1], Player, oUF_Basic.unitframe.pet.position[3], oUF_Basic.unitframe.pet.position[4], oUF_Basic.unitframe.pet.position[5])

local target = oUF:Spawn("target", "Target")
target:SetPoint(oUF_Basic.unitframe.target.position[1], oUF_Basic.unitframe.target.position[2], oUF_Basic.unitframe.target.position[3], oUF_Basic.unitframe.target.position[4], oUF_Basic.unitframe.target.position[5])

local tot = oUF:Spawn("targettarget", "TargetsTarget")
tot:SetPoint(oUF_Basic.unitframe.targetstarget.position[1], Target, oUF_Basic.unitframe.targetstarget.position[3], oUF_Basic.unitframe.targetstarget.position[4], oUF_Basic.unitframe.targetstarget.position[5])

local focus = oUF:Spawn("focus", "Focus")
focus:SetPoint(oUF_Basic.unitframe.focus.position[1], oUF_Basic.unitframe.focus.position[2], oUF_Basic.unitframe.focus.position[3], oUF_Basic.unitframe.focus.position[4], oUF_Basic.unitframe.focus.position[5])

local focustarget = oUF:Spawn("focustarget", "oUF_FocusTarget")
focustarget:SetPoint(oUF_Basic.unitframe.focustarget.position[1], oUF_Basic.unitframe.focustarget.position[2], oUF_Basic.unitframe.focustarget.position[3], oUF_Basic.unitframe.focustarget.position[4], oUF_Basic.unitframe.focustarget.position[5])

local party = oUF:Spawn("header", "oUF_Party")   
party:SetPoint(oUF_Basic.unitframe.party.position[1], oUF_Basic.unitframe.party.position[2], oUF_Basic.unitframe.party.position[3], oUF_Basic.unitframe.party.position[4], oUF_Basic.unitframe.party.position[5])
party:SetManyAttributes("showParty", true, "yOffset", -50)

local partypet1 = oUF:Spawn("partypet1", "oUF_PartyPet1")   
partypet1:SetPoint(oUF_Basic.unitframe.party.position[1], oUF_Basic.unitframe.party.position[2], oUF_Basic.unitframe.party.position[3], oUF_Basic.unitframe.party.position[4] + 50, oUF_Basic.unitframe.party.position[5] - 100)

local partypet2 = oUF:Spawn("partypet2", "oUF_PartyPet2")   
partypet2:SetPoint(oUF_Basic.unitframe.party.position[1], oUF_Basic.unitframe.party.position[2], oUF_Basic.unitframe.party.position[3], oUF_Basic.unitframe.party.position[4] + 50, oUF_Basic.unitframe.party.position[5] - 206)

local partypet3 = oUF:Spawn("partypet3", "oUF_PartyPet3")   
partypet3:SetPoint(oUF_Basic.unitframe.party.position[1], oUF_Basic.unitframe.party.position[2], oUF_Basic.unitframe.party.position[3], oUF_Basic.unitframe.party.position[4] + 50, oUF_Basic.unitframe.party.position[5] - 312)

local partypet4 = oUF:Spawn("partypet4", "oUF_PartyPet4")   
partypet4:SetPoint(oUF_Basic.unitframe.party.position[1], oUF_Basic.unitframe.party.position[2], oUF_Basic.unitframe.party.position[3], oUF_Basic.unitframe.party.position[4] + 50, oUF_Basic.unitframe.party.position[5] - 419)

-- Caellian
local tevent = CreateFrame("Frame", nil, Player)
-- tevent:SetScript('OnEvent', function(self)
--   if (InCombatLockdown()) then
--     self:RegisterEvent('PLAYER_REGEN_ENABLED')
--   else
--     self:UnregisterEvent('PLAYER_REGEN_ENABLED')
--       if (GetNumRaidMembers() > 5) then
-- 	 party:Hide()
--       else
-- 	 party:Show()
--       end
--  end
-- end)

if (oUF_Basic.show.party) then
     party:Show()
else
     party:Hide()
end

if (oUF_Basic.show.pvpicons) then
    tevent:SetScript("OnUpdate", function(self, elapsed)
        if (IsPVPTimerRunning()) then
            time = GetPVPTimer()
        else
            time = nil
        end

        if (time) then
            time = time - elapsed * 1000
            local timeLeft = time
            if (timeLeft < 0) then
                --PlayerPVPTimer:SetText("")
            end
            --PlayerPVPTimer:SetFormattedText(SecondsToTimeAbbrev(floor(timeLeft/1000)))
        else
            --PlayerPVPTimer:SetText("")
        end
    end)
end

tevent:RegisterEvent('PLAYER_LOGIN')
tevent:RegisterEvent("CVAR_UPDATE")
tevent:RegisterEvent('RAID_ROSTER_UPDATE')
tevent:RegisterEvent('PARTY_LEADER_CHANGED')
tevent:RegisterEvent('PARTY_MEMBERS_CHANGED')